package com.sayimo.maker.mall.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.base.dao.BasePhotoInterfaceDao;
import com.sayimo.maker.base.vo.BasePhoto;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.mall.goods.dao.EnterpriseCommentDao;
import com.sayimo.maker.mall.goods.service.EnterpriseCommentService;
import com.sayimo.maker.mall.goods.vo.EnterpriseComment;


/**
 * @Copyright (c) 2016 广东省尚一互联网有限公司
 * @Project: maker-mall
 * @ClassName: EnterpriseCommentServiceImpl.java
 * @Description: 商家评论
 * @Author:	tanxin
 * @Date:2017年2月16日,下午3:01:25
 * @Version 1.0.0
 */
@Service(value="enterpriseCommentService")
public class EnterpriseCommentServiceImpl implements EnterpriseCommentService{
	
	private static Log logger = LogFactory.getLog(EnterpriseCommentServiceImpl.class);
	
	@Resource(name="enterpriseCommentDao") 
	private EnterpriseCommentDao enterpriseCommentDao;
	@Resource(name="basePhotoDao")
	private BasePhotoInterfaceDao basePhotoDao;
	
	/**
	 * @Package com.sayimo.maker.mall.goods.service.impl
	 * @Author TANXIN
	 * @MethodName:addEnterprisePointPraise
	 * @Description:商家点赞
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @Date 2017年2月16日,上午11:55:01
	 */
	@Override
	public Map<String, Object> addEnterprisePointPraise(
			HttpServletRequest request) throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("providerId",Integer.parseInt(request.getParameter("providerId")));
		try {
			Integer count = enterpriseCommentDao.addEnterprisePointPraise(paramMap);
			if(count > 0){
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, Constant.SUCCESS);
			}else{
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
			}
		} catch (DaoException e) {
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			logger.error(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}
	/**
	 * @Package com.sayimo.maker.mall.goods.service.impl
	 * @Author TANXIN
	 * @MethodName:addEnterpriseComment
	 * @Description:添加商家评论
	 * @param request
	 * @return
	 * @throws ServiceException
	 * @Date 2017年2月16日,下午2:24:47
	 */
	@Override
	public Map<String, Object> addEnterpriseComment(
			HttpServletRequest request) throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		EnterpriseComment enterpriseComment=new EnterpriseComment();
		
		enterpriseComment.setProviderId(Integer.parseInt(request.getParameter("providerId")));
		enterpriseComment.setCustomerId((Integer) request.getAttribute("customerId"));
		enterpriseComment.setCommentLevel(Integer.parseInt(request.getParameter("commentLevel")));
		enterpriseComment.setCommentContent(request.getParameter("commentContent"));
		String photoIds=request.getParameter("photoIds");
		if(photoIds != null && !"".equals(photoIds)){
			enterpriseComment.setPhotoIds(photoIds);
		}
		
		try {
			Integer count = enterpriseCommentDao.addEnterpriseComment(enterpriseComment);
			if(count > 0){
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, Constant.SUCCESS);
			}else{
				resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
				resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			}
		} catch (DaoException e) {
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			logger.error(e);
			throw new ServiceException(e);
		}
		return resultMap;
	}
	/**
	 * @Package com.sayimo.maker.mall.goods.service.impl
	 * @Author TANXIN
	 * @MethodName:queryEnterpriseComment
	 * @Description:查询商家评论
	 * @param customerId
	 * @param pageNow
	 * @param pageSize
	 * @param type
	 * @return
	 * @throws ServiceException
	 * @Date 2017年2月16日,下午2:54:27
	 */
	public Map<String, Object> queryEnterpriseComment(Integer providerId,String commentLevel,Integer pageNow,Integer pageSize) throws ServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<EnterpriseComment> enterpriseCommentList = null;
		
		if(providerId != null && !"".equals(providerId)){
			paramMap.put("providerId", providerId);
		}
		if(commentLevel != null && !"".equals(commentLevel)){
			paramMap.put("commentLevel", Integer.valueOf(commentLevel));
		}
		if(pageNow != null && pageNow == 0){
			paramMap.put("pageNow", 0);
		}else if(pageNow != null && pageNow > 0){
			paramMap.put("pageNow", (pageNow-1)*pageSize);
		}
		if(pageSize != null){
			paramMap.put("pageSize", pageSize);
		}
		
		try {
			enterpriseCommentList = enterpriseCommentDao.queryEnterpriseComment(paramMap);
			if(enterpriseCommentList!=null){
				for(int i=0;i<enterpriseCommentList.size();i++){
					if(enterpriseCommentList.get(i).getPhotoIds()!=null&&!"".equals(enterpriseCommentList.get(i).getPhotoIds())){
						List<BasePhoto> basePhotoList=basePhotoDao.selectAllById(enterpriseCommentList.get(i).getPhotoIds());
						enterpriseCommentList.get(i).setBasePhotoList(basePhotoList);
					}
				};
			}
			
		} catch (DaoException e) {
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR, Constant.SYSTEM_ERROR);
			logger.error(e);
			throw new ServiceException(e);
		}
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("enterpriseCommentList", enterpriseCommentList);
		resultMap.put(Constant.DATA, dataMap);
		resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
		return resultMap;
	}

}
